/*------------------------------------------------------------------------------------------------------------------------------------------
--		SOURCE FILE:	Config.cpp		-	Represents the "Physical Layer" of the ISO model.  Handles initializing and configuring the 
--											serial port.
--
--		PROGRAM:		Wireless Modem Protocol Driver
--
--		FUNCTIONS:		HANDLE initializePort(LPCTSTR portName, LPDCB pDcb)
--						void setUserPrefs(HANDLE hPort, LPDCB pDcb);
--						
--
--		DATE:			September 26, 2011
--
--		REVISIONS:		2.0
--
--		DESIGNER:		Mike Zobac/Chris Sim
--
--		PROGRAMMER		Mike Zobac/ Chris Sim
--
--		NOTES:			Provides functions which call the Windows API CreateFile, SetupComm, GetCommState, and SetCommState functions. 
--						All of these functions are passed a pointer to a TTYINFO struct.  This structure contains all of the variables which
--						would otherwise have to be global, or extern globals. 
--------------------------------------------------------------------------------------------------------------------------------------------*/
#include "Config.h"




/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:		initializePort
--
--		DATE:			September 26, 2011
--
--		DESIGNER:		Mike Zobac/Chris Sim
--
--		PROGRAMMER:		Mike Zobac/Chris Sim
--
--		INTERFACE:		HANDLE initializePort(LPCTSTR portName, LPDCB pDcb)
--
--		RETURNS:		HANDLE to the port
--
--		NOTES:			Use this file to set up the port, and initialize a DCB.
--------------------------------------------------------------------------------------------------------------------------------------------*/
HANDLE initializePort(PTTYINFO info)
{
    
	info->hPort = CreateFile(info->portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
    if(info->hPort == INVALID_HANDLE_VALUE)
	{
		MessageBox (NULL, "Error opening COM port","",MB_OK);
		return NULL;
	}
	
	if(!SetupComm(info->hPort, 2, 2))
    {
		MessageBox (NULL, "SetupComm failed", "", MB_OK);
		return(NULL);
	}
	
    if(!GetCommState(info->hPort, &info->cc.dcb))
    {
		MessageBox (NULL, "GetCommState failed", "", MB_OK);
		return(NULL);
	}
	
	BuildCommDCB("9600, N, 8, 1", &info->cc.dcb);

	if(!SetCommState(info->hPort, &info->cc.dcb))
	{
		MessageBox (NULL, "setCommState failed", "",MB_OK);
	}

	return info->hPort;
}


/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:		Set User Prefs
--
--		DATE:			September 26, 2011
--
--		DESIGNER:		Mike Zobac
--
--		PROGRAMMER:		Mike Zobac
--
--		INTERFACE:		void setUserPrefs(HANDLE hPort, LPDCB pDcb)
--
--		RETURNS:		void
--
--		NOTES:			Use this function to set the port to specified user settings, gathered by the Application layer, and written into
--						the DCB passed in this function.
--------------------------------------------------------------------------------------------------------------------------------------------*/
void setUserPrefs(PTTYINFO info)
{
	if(!SetCommState(info->hPort, &info->cc.dcb))
    {
		MessageBox (NULL, "setCommState failed", "",MB_OK);
	}
}


